package com.wechat.back.controller;

import com.wechat.back.anno.OptLog;
import com.wechat.back.pojo.tools.Result;
import com.wechat.back.utils.ThreadLocalUtil;
import jakarta.websocket.OnOpen;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/image")
public class FileUploadController {

    @Value(value = "${upload.avatarPath}")
    private String avatarPath;

//    @OptLog(optModule = "上传图片模块",optType = "上传", optDesc = "上传头像")
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws IOException {

        String originalFilename = file.getOriginalFilename();
        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        if (username == null) {
            username = "admin";
        }
        String filename = username+"-"+UUID.randomUUID() +originalFilename.substring(originalFilename.lastIndexOf("."));
        file.transferTo(new File(avatarPath+filename));
        String path = "upload/avatar" + File.separator + filename;
        if (path.contains("\\")) {
            path = path.replace("\\", "/");
        }
        return Result.success("http://localhost:8080/"+path);
    }

    @Value("${upload.iconPath}")
    private String iconPath;

    @OptLog(optModule = "上传图片模块",optType = "上传", optDesc = "上传图标")
    @PostMapping("/icon")
    public Result<String> icon(MultipartFile file) throws IOException {

        String iconImage = file.getOriginalFilename();
//        Map<String, Object> map = ThreadLocalUtil.get();
//        String username = (String) map.get("username");
        String username = "admin";
        String filename = username+"-"+UUID.randomUUID() +iconImage.substring(iconImage.lastIndexOf("."));
        file.transferTo(new File(iconPath+filename));
        String path = "upload/icons" + File.separator + filename;
        if (path.contains("\\")) {
            path = path.replace("\\", "/");
        }
        return Result.success("http://localhost:8080/"+path);
    }

    @Value("${upload.bgPath}")
    private String bgPath;

    @OptLog(optModule = "上传图片模块",optType = "上传", optDesc = "上传背景图片")
    @PostMapping("/bg")
    public Result<String> bg(MultipartFile file) throws IOException {
        String iconImage = file.getOriginalFilename();
        String username = "admin";
        String filename = username+"-"+UUID.randomUUID() +iconImage.substring(iconImage.lastIndexOf("."));
        file.transferTo(new File(bgPath+filename));
        String path = "upload/bg" + File.separator + filename;
        if (path.contains("\\")) {
            path = path.replace("\\", "/");
        }
        return Result.success("http://localhost:8080/"+path);
    }
}
